﻿module Sustainalytics.EsgRatings {

    import Company = Sustainalytics.Company;

    export class QualitativePerformanceController {

        public esgControversyIndicators: EsgControversyIndicatorDto[];
        public esgControversyIndicatorsDic: Common.Dictionary<EsgControversyIndicatorDto[]> = new Common.Dictionary<EsgControversyIndicatorDto[]>();        


        constructor(
            private $scope: Company.ICompanyBasedScope,
            private $q: ng.IQService,
            private $timeout: ng.ITimeoutService,
            private $filter: ng.IFilterService,
            private EsgControversyIndicatorService: IEsgControversyIndicatorService,
            private NumberUtilityService: Common.NumberUtilityService,
            private $rootScope) {

            var ctx = this;
            ctx.init();
        }

        private init(): ng.IPromise<any> {
            var ctx = this;

            var promiseIndicators = ctx.getEsgControversyIndicators();

            return promiseIndicators;
        }

        private getEsgControversyIndicators(): ng.IPromise<EsgControversyIndicatorDto[]> {
            var ctx = this;
            var promise = ctx.EsgControversyIndicatorService.get(ctx.$scope.currentCompany.id);

            promise.then(function (res) {
                ctx.$timeout(function () {
                    ctx.$scope.$apply(function () {
                        // clear dictionary
                        ctx.esgControversyIndicatorsDic = new Common.Dictionary<EsgControversyIndicatorDto[]>();

                        // set values
                        ctx.esgControversyIndicators = res;

                        // fill dictionary
                        $.each(ctx.esgControversyIndicators, function (index, ind) {
                            // get indicators by category
                            var lst: EsgControversyIndicatorDto[] = ctx.esgControversyIndicatorsDic.getValue(ind.answerCategory.toString());

                            // create indicators list
                            if (!lst) {
                                lst = new Array();
                                ctx.esgControversyIndicatorsDic.add(ind.answerCategory.toString(), lst);
                            }

                            // add indicator to list
                            lst.push(ind);
                        });
                    });
                });
            },() => {

                ga('create', 'UA-4379897-6', {
                    'userId': ctx.$rootScope.userInfo.userId, 'cookieDomain': 'none', 'name': 'globalAccess'
                });

                ga('globalAccess.send', 'exception', { 'exDescription': 'Esg Ratings: Qualitative Performance Controller -> Error loading Controversy Indicators' });
            });

            return promise;
        }

        // filter controversy indicators by category
        public getEsgControversyIndicatorsByCategory(category: number): EsgControversyIndicatorDto[] {
            var ctx = this;
            var categoryInd = ctx.esgControversyIndicatorsDic.getValue(category.toString());
            return categoryInd;
        }

        // get last category indicators
        public getEsgControversyIndicatorsInLastCategory(): EsgControversyIndicatorDto[] {
            var ctx = this;
            var lastKey = ctx.getEsgControversyIndicatorsLastCategory();
            if (!lastKey)
                return null;

            var indicators = ctx.esgControversyIndicatorsDic.getValue(lastKey.toString());
            return indicators;
        }

        // get last category
        public getEsgControversyIndicatorsLastCategory(): number {
            var ctx = this;
            var keys = ctx.esgControversyIndicatorsDic.getKeys();
            if (!keys)
                return null;

            var lastKey = keys[keys.length - 1];
            return parseInt(lastKey) || 0;
        }

        // whether has indicators of specific category
        public hasEsgControversyIndicatorsInCategory(category: number): boolean {
            var ctx = this;
            var indicators = ctx.getEsgControversyIndicatorsByCategory(category);
            return indicators && indicators.length > 0;
        }

        public getEsgPosition(): any {
            return EsgPositionEnum;
        }

        public getEsgPositionName(esgPosition: EsgPositionEnum): string {
            return EsgUtility.getEsgPositionName(esgPosition);
        }
    }


    export var qualitativePerformanceControllerName = 'QualitativePerformanceController';

    angular.module(esgRatingsModuleName).controller(qualitativePerformanceControllerName, QualitativePerformanceController);
} 